package com.kalaqia.permission.aspect;

import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;

/**
 * 检查权限的工具类
 * <p/>
 * Created by wangchenlong on 16/1/26.
 */
public class PermissionsChecker {
    private final Context mContext;
    private static PermissionsChecker instance;

    private PermissionsChecker(Context context) {
        mContext = context.getApplicationContext();
    }

    public static PermissionsChecker getInstance(Context context) {
        if (instance == null) {
            instance = new PermissionsChecker(context);
        }
        return instance;
    }

    // 判断权限集合
    public boolean isHasAllPermissions(String... permissions) {
        for (String permission : permissions) {
            if (lacksPermission(permission)) {
                return false;
            }
        }
        return true;
    }

    // 判断权限集合
    public boolean isHasAllPermissions(int... permissions) {
        for (int permission : permissions) {
            if (permission == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }

    // 判断是否缺少权限
    public boolean lacksPermission(String permission) {
        return ContextCompat.checkSelfPermission(mContext, permission) ==
                PackageManager.PERMISSION_DENIED;
    }
}
